//
//  MenkSoftSegment.m
//  分页
//
//  Created by Burn on 2019/4/23.
//  Copyright © 2019 menksoft. All rights reserved.
//

#define kSCOLOR [UIColor colorWithRed:21 / 255.0 green:126 / 255.0 blue:251 / 255.0 alpha:1]

#import "MenkSoftSegment.h"



@interface MenkSoftSegment ()

@property(strong,nonatomic) NSArray *titles;
@property(strong,nonatomic) NSMutableArray *labels;
@property(assign,nonatomic) NSInteger currentIndex;

@end

@implementation MenkSoftSegment

- (instancetype)initWithFrame:(CGRect)frame andTItles:(NSArray *)titles{
    if (self = [super initWithFrame:frame]) {
        self.titles = titles;
        
    }
    return self;
}


- (void)creatSubViews{
    UILabel *label = [[UILabel alloc]init];
    label.frame = self.bounds;
    label.layer.cornerRadius = 3;
    label.layer.masksToBounds = YES;
    label.layer.borderWidth = 1;
    label.layer.borderColor = kSCOLOR.CGColor;
    [self addSubview:label];
    if (self.titles.count <= 1) {
        return;
    }
    for (int i = 0; i < self.titles.count - 1; i++) {
        UIView *line = [[UIView alloc]init];
        line.backgroundColor = kSCOLOR;
        line.frame = CGRectMake(self.bounds.size.width / self.titles.count * (i + 1), 0, 1, self.bounds.size.height);
        [self addSubview:line];
    }
    for (int i = 0; i < self.titles.count; i++) {
        UILabel *title = [[UILabel alloc]init];
        title.text = self.titles[i];
        title.numberOfLines = 0;
        title.transform = CGAffineTransformScale(title.transform, -1, 1);
        title.transform =  CGAffineTransformRotate(title.transform, M_PI / 2);
        title.font = [UIFont fontWithName:@"MenksoftQagan_mirror" size:14];
        title.tag = 3000 + i;
        title.backgroundColor = [UIColor whiteColor];
        title.textColor = [UIColor blueColor];
        title.frame = CGRectMake(i * (self.bounds.size.width / self.titles.count ) + 1 , 1, (i == self.titles.count - 1 ? (self.bounds.size.width / self.titles.count) - 2 : (self.bounds.size.width / self.titles.count) - 1) , self.bounds.size.height - 2);
        [self addSubview:title];
        
        title.userInteractionEnabled = YES;
        UITapGestureRecognizer *labelTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(titleClick:)];
        [title addGestureRecognizer:labelTapGestureRecognizer];
        [self.labels addObject:title];
        
        if (i == 0 || i == self.titles.count - 1) {
            title.layer.cornerRadius = 2;
            title.layer.masksToBounds = YES;
        }
        
        if (i == self.originalIndex) {
            title.textColor = [UIColor whiteColor];
            title.backgroundColor = kSCOLOR;
            self.currentIndex = self.originalIndex;
        }
    }
}

- (void)titleClick:(UIGestureRecognizer *)ges{
    UILabel *oldLabel = self.labels[self.currentIndex];
    oldLabel.textColor = kSCOLOR;
    oldLabel.backgroundColor = [UIColor whiteColor];
    //  当前label
    UILabel *label = (UILabel *)ges.view;
    self.currentIndex = label.tag - 3000;
    label.textColor = [UIColor whiteColor];
    label.backgroundColor = kSCOLOR;
    
    if ([self.delegate respondsToSelector:@selector(didchangeForIndex:)]) {
        [self.delegate didchangeForIndex:self.currentIndex];
    }
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    
}

- (void)willMoveToSuperview:(UIView *)newSuperview{
    [super willMoveToSuperview:newSuperview];
    [self creatSubViews];
}

- (NSMutableArray *)labels{
    if (!_labels) {
        _labels = [NSMutableArray array];
    }
    return _labels;
}

@end
